
GamePanel = {};
local this = GamePanel;

local transform;
local gameObject;


function GamePanel.New(data)
    panelMgr:CreatePanel("Game",nil);
    this.roomdata = data;
end

function GamePanel.Awake(obj)
    gameObject=obj;
    transform=obj.transform;

end

function GamePanel.Start()
    
    this.behaviour = transform:GetComponent('LuaBehaviour');
    ResourcesFactory.LoadAsset(this.behaviour);--加载资源

    
    this.roomIdText=transform:Find("RoomId"):GetComponent('UILabel');--房间号文本
    this.readyBtn=transform:Find("readyBtn").gameObject;--准备按钮

    --出牌按钮
    this.putview=transform:Find("putview").gameObject;
    this.putoutBtn=transform:Find("putview/putoutBtn").gameObject;
    this.tipsBtn=transform:Find("putview/tipsBtn").gameObject;
    this.passBtn=transform:Find("putview/passBtn").gameObject;

    --叫地主按钮
    this.robview=transform:Find("robview").gameObject;
    this.robBtn=transform:Find("robview/robBtn").gameObject;
    this.noRobBtn=transform:Find("robview/passBtn").gameObject;

    --初始化players座位
    this.players={};
    --遍历找到玩家
    for i=1,3 do
        this.players[i] = Player.new(transform:Find("Players/Player"..i).gameObject);
    end

    --处理data中，房间号，座位号，其他玩家等
    this.roomIdText.text="房间号："..this.roomdata.roomId;

    for i,v in ipairs(this.roomdata.users) do
        local pos=this.GetSeatPos(v.seatId);
        this.players[pos]:Init(v);
        if(this.roomdata.seatId==v.seatId)then
            if(v.isReady)then
                this.readyBtn:SetActive(false);
            else
                this.readyBtn:SetActive(true);
            end
        end
    end

    this.AddClick();
end

--添加按钮绑定事件
function GamePanel.AddClick()
    this.behaviour:AddClick(this.readyBtn,this.OnReadyClick);

    this.behaviour:AddClick(this.putoutBtn,this.OnPutOutClick);
    this.behaviour:AddClick(this.tipsBtn,this.OnTipsClick);
    this.behaviour:AddClick(this.passBtn,this.OnPassClick);

    this.behaviour:AddClick(this.robBtn,this.OnRobClick);
    this.behaviour:AddClick(this.noRobBtn,this.OnNotRotClick);
end




------------------------------生命周期-----------------------------------------
--新玩家进入
function GamePanel.JoinPlayer(newUser)
    local pos = this.GetSeatPos(newUser.seatId);--设置新玩家的座位号
    this.players[pos]:Init(newUser);--初始化玩家
end

--玩家准备
function GamePanel.PlayerReady(seatId)

    this.GetPlayer(seatId):ShowReady(true);
end

--玩家发牌，本机玩家自己接到pais
function GamePanel.ReceivePai(pais)
    this.players[1]:SetPais(pais);

end

--轮到玩家抢地主
function GamePanel.TurnToRob(seatId)
    if(this.roomdata.seatId==seatId)then
        this.ShowRoView(true);
    else
        this.GetPlayer(seatId):ShowRoTurn();
    end

end

--展示不抢地主
function GamePanel.ShowNotRob(seatId)
    this.GetPlayer(seatId):ShowNotRob();
end

--广播地主
function GamePanel.RobLandLord(seatId,pais)
    log(#pais)
    --状态全部清空
    for i,v in ipairs(this.players) do
        v:SetState();
    end
    --抢到地主，开始出牌
    this.GetPlayer(seatId):ShowLandLord(true);
    --获得手牌
    if (seatId==this.roomdata.seatId) then
        this.players[1]:SetPais(pais);
        this.ShowPutView(true);
    end
    log("地主加派")
end

--轮到玩家出牌
function GamePanel.TurnToPut(seatId)
    if(this.roomdata.seatId==seatId)then
        this.ShowPutView(true);
    else
        this.GetPlayer(seatId):ShowPutTurn();
    end
end


--玩家出牌
function GamePanel.PutCard(data)  
    this.GetPlayer(data.seatId):ShowPutCard(data.pais);
end

--玩家要不起
function GamePanel.ShowNotRob(seatId)
    this.GetPlayer(seatId):ShowNotPut();
end

--游戏结束
function GamePanel.GameOver( data )
    for i=0,3 do
        this.GetPlauyer(i):SetState();
        this.GetPlayer(i):SetScore(data.results[i].score);
    end
end

--重新开始
function GamePanel.Restart(seatId)
    if(this.roomdata.seatId==seatId)then
        panelMgr:ClosePanel("Over");

        for i=1,3 do
            this.players[i]:Clear();
        end
    end

    this.GetPlayer(seatId):ShowReady();

end


---------------------------点击事件-----------------------------------

--玩家点击准备按钮
function GamePanel.OnReadyClick()
    this.readyBtn:SetActive(false);
    Network.Send(Protocal.ReqReady)
end
--抢地主点击
function GamePanel.OnRobClick()
    this.ShowRoView(false);
    Network.Send(Protocal.ReqRob)
    log("发送抢地主")
end
--不抢点击
function GamePanel.OnNotRotClick()
    this.ShowRoView(false);
    Network.Send(Protocal.ReqNotRob)
end

--出牌点击
function GamePanel.OnPutOutClick()
    log("点击")
    --if (Card.shoot==true) then
        log("点击")
        this.ShowPutView(false);
        this.players[1]:PutOut();
    --end
   
end
--提示点击
function GamePanel.OnTipsClick()
    this.ShowPutView(false);
    this.players[1]:Tips();
end
--要不起点击
function GamePanel.OnPassClick()
    this.ShowPutView(false);
    Network.Send(Protocal.ReqPassCard)
end



--------------------工具方法----------------------
--得到seatId的板凳序号
function GamePanel.GetSeatPos(seatId)
    local offset = seatId-this.roomdata.seatId;
    if(offset<0)then
        offset=offset+3;
    end
    return offset+1;
end

--返回玩家
function GamePanel.GetPlayer(seatId)
    local pos = this.GetSeatPos(seatId);
    return this.players[pos];
end

--显示叫地主按钮
function GamePanel.ShowRoView(isOn)
    this.robview:SetActive(isOn);
end

--显示出牌按钮
function GamePanel.ShowPutView(isOn)
    this.putview:SetActive(isOn);
end